﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Microsoft.SqlServer.Management;
using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlServer.Management.Common;
using Hafic.Stock.Management.Common;

namespace DXEasyAccount.Security.Forms
{
    public partial class frmBackup : DevExpress.XtraEditors.XtraForm
    {
        #region "Define Variable"
        Server srv;
        ServerConnection conn;
        private string _strfileName;
        #endregion

        #region "Construction"
        public frmBackup()
        {
            InitializeComponent();
        }
        #endregion

        #region "Private Method"
        private void doBackup()
        {
            //srv = new Server();
            string sqlServerInstance;
            string sqlDatabaseName;
            string sqlUserName;
            string sqlPassword;
            string[] arrSqlServer = clsVariable.strSqlConnectionString.Split(';');
            sqlServerInstance = arrSqlServer[0].Replace("Data Source", "").Replace("=","").Trim();
            sqlDatabaseName = arrSqlServer[1].Replace("Initial Catalog", "").Replace("=","").Trim();
            sqlUserName = arrSqlServer[2].Replace("User Id", "").Replace("=", "").Trim();
            sqlPassword = arrSqlServer[3].Replace("Password", "").Replace("=", "").Trim();

            conn = new ServerConnection(sqlServerInstance, sqlUserName, sqlPassword);
            srv = new Server(conn);

            Backup bkp = new Backup();
            this.Cursor = Cursors.WaitCursor;
            _strfileName = String.Format(@"{0}\{1}", txtFolder.EditValue,txtFileName.EditValue);
            bkp.Devices.AddDevice(_strfileName, DeviceType.File);
            bkp.Database = sqlDatabaseName;
            bkp.Action = BackupActionType.Database;
            bkp.Initialize = true;
            bkp.PercentCompleteNotification = 10;
            bkp.PercentComplete += new PercentCompleteEventHandler(ProgressEventHandler);
            try
            {
                bkp.SqlBackup(srv);
                XtraMessageBox.Show(
                    "Sao lưu thành công dữ liệu vào đường dẫn: " + _strfileName, 
                    "Thông tin");
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.ToString());
            }
            finally
            {
                this.Cursor = Cursors.Default;
                this.processBar.EditValue = 0;
            }

        }
        #endregion

        #region "Form Event"
        private void frmBackup_Load(object sender, EventArgs e)
        {
            txtFileName.Text = String.Format("QLK.{0}.{1}.{2}.{3}.{4}.bak",
                                        DateTime.Now.ToString("dd"),
                                        DateTime.Now.ToString("MM"),
                                        DateTime.Now.ToString("yyyy"),
                                        DateTime.Now.ToString("hh"),
                                        DateTime.Now.ToString("mmm"));
        }
        #endregion
        #region "Process Bar Event"
        public void ProgressEventHandler(object sender, PercentCompleteEventArgs e)
        {
            this.processBar.EditValue = e.Percent;
        }

        #endregion

        #region "Button Event"
        private void txtFolder_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            folderBrowserDialog.ShowDialog();
            if (!String.IsNullOrEmpty(folderBrowserDialog.SelectedPath))
            {
                txtFolder.EditValue = folderBrowserDialog.SelectedPath;
            }
        }

        

        private void btnOk_Click(object sender, EventArgs e)
        {
            doBackup();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        #endregion
    }
}